import { Store, Model } from "@/utils/model/index";
import { AxiosProxy } from "@/utils/model/proxy";
import axios from '@/utils/_require';

export class UsersModel extends Model {
  // model = SalesTeamModel

  autoLoad = true

  constructor(data = {}) {
    super(data)
  }

  proxy = new AxiosProxy({
    actions: {
      read: {
        url: "/admin/acl/user",
        method: "get"
      },
      update: {
        url: '/admin/acl/user/update',
        method: "put"
      },
      delete: {
        url: '/admin/acl/user/remove/<%= id %>',
        method: "delete"
      }
    }
  })

  // 分配权限
  async doAssignRole() {
    try {
      this.vm.status = 1
      const result = await axios({
        url: '/admin/acl/user/doAssignRole',
        method: 'post',
        data: {
          roleIdList: this.vm.data.checkedRole.map(item => item.id),
          userId: this.vm.data.id
        }
      })
      this.vm.status = 2
    } catch (error) {
      this.vm.status = 3
    }
  }

  // 更新用户
  async update() {

    this.vm.status = 1

    const operation = {
      type: 'update',
      data: this.vm.data
    }

    // 本地执行器
    await this.execute(operation)
  }


}

export class UsersStore extends Store {
  model = UsersModel

  autoLoad = true

  constructor(data = []) {
    super(data)
  }

  proxy = new AxiosProxy({
    actions: {
      read: {
        url: "/admin/acl/user/<%= page %>/<%= size %>",
        method: "get"
      }
    },
  })

  async load(option) {
    // this.controller?.abort()
    // this.controller = new AbortController()
    this.preOperation?.abort()

    // filter.delay = (filter.page || 1) * 1000 
    const operation = {
      type: "read",
      ...option
    }
    return this.execute(operation)
  }

}